﻿<div ng-controller="Customer_EditC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">修改客户</h1>
    </div>
    <div class="modal fade" id="shangchuan" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form id="myupload" action={{myurl}} method="post" enctype="multipart/form-data">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabe2">上传用户凭证 </h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-group btn">
                            <!--<input type="label" name="label" id="label" class="form-control" placeholder="请选择文件" /><br />-->
                            <label style="margin-right: 10px;">请选择文件</label>
                            <input type="file" accept=".jpg,.jpeg,.png" name="file1" id="fileupload" class="form-control" value="" /><br />
                        </div>
                        <div class="progress">
                            <span class="bar"></span><span class="percent">0%</span>
                        </div>
                        <!-- 显示已上传的文件名 -->
                        <div class="files"></div>
                        <!-- 显示已上传的图片-->
                        <div class="showimg"></div>
                    </div>
                    <div class="modal-footer">
                        <!--<button type="button" class="btn btn-success" ng-disabled="myform1.$invalid" ng-click="okchange()">确认提交</button>-->
                        <input type="button" class="btn btn-default" ng-click="gosubmit2()" value="提交" />
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <span class="h4">修改客户信息</span>
                        </div>
                        <div class="panel-body">
                            <div class="form-group">
                                <label>简称 <em class="text-muted">(2-15字符)(必填)</em></label>
                                <input type="text" class="form-control" required ng-model="data.ShortName" minlength="2" maxlength="15">
                            </div>
                            <div class="form-group">
                                <label>名称(必填)</label>
                                <input type="text" class="form-control" required ng-model="data.Name">
                            </div>
                            <div class="form-group">
                                <label style="margin-right: 10px;">是否个人账户</label>
                                <label class="i-switch bg-info">
                                    <input type="checkbox" ng-model="data.IsPersonal">
                                    <i></i>
                                </label>
                            </div>
                            <div class="form-group">
                                <label>地区 </label>
                                <div class="input-group ">
                                    <span class="input-group-addon">省</span>
                                    <select class="form-control" ng-options="x.FillName for x in districts" ng-model="selectedDistrict1"></select>
                                    <span class="input-group-addon">市</span>
                                    <!--<select class="form-control" ng-options=" y as y.Name for y in selectedDistrict1.Children track by y.Id" ng-model="data.District" >-->
                                    <!--<option ng-repeat="y in selectedDistrict1.Children" value="{{y.Id}}">{{y.Name}}</option>-->
                                    <!--</select>-->
                                    <select required class="form-control" ng-options="y as y.Name for y in selectedDistrict1.Children track by y.Id" ng-model="data.District"></select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>地址</label>
                                <input type="text" class="form-control" ng-model="data.Address">
                            </div>
                            <!--<div class="form-group">
                                <label>联系人</label>
                                <input type="text" class="form-control" ng-model="data.Contacts">
                            </div>
                            <div class="form-group">
                                <label>电话(必填)</label>
                                <input type="text" class="form-control" ng-model="data.ContactPhone" required ng-pattern="/^[1][3-8]\d{9}$/">
                            </div>
                            <div class="form-group">
                                <label>财务</label>
                                <input type="text" class="form-control" ng-model="data.Accountant">
                            </div>
                            <div class="form-group">
                                <label>财务电话</label>
                                <input type="text" class="form-control" ng-model="data.AccountantPhone" ng-pattern="/^[1][3-8]\d{9}$/">
                            </div>-->
                            <!--------------------动态加载联系人行 Start--------------------------------------------------->

                            <div class="form-group">
                                <div id="Contacts-wraper">
                                    <button type="button" class="btn btn-default" ng-click=" AddContact()">新增</button>
                                    <div class="Contacts-inner" ng-repeat="x in data.CustomerContacts">
                                        <label>联系人{{$index + 1}}</label>
                                        <input type="text" ng-model="x.ContactUser">
                                        <label>电话(必填)</label>
                                        <input type="text" ng-model="x.ContactPhone" required ng-pattern="/^[1][3-8]\d{9}$/">
                                        <button type="button" class="btn btn-default" ng-click="remove($index)">删除</button>
                                    </div>

                                  
                                </div>
                            </div>



                            <div class="form-group">
                                <div id="Contacts-wraper">
                                    <button type="button" class="btn btn-default" ng-click=" AddAccountantContact()">新增</button>
                                    <div class="Contacts-inner" ng-repeat="x in data.AccountantContacts">
                                        <label>财务{{$index + 1}}</label>
                                        <input type="text" ng-model="x.ContactUser">
                                        <label>财务电话</label>
                                        <input type="text" ng-model="x.ContactPhone" ng-pattern="/^[1][3-8]\d{9}$/">
                                        <button type="button" class="btn btn-default" ng-click="removeAccountant($index)">删除</button>
                                    </div>

                                    
                                </div>
                            </div>
                            <!--------------------End--------------------------------------------------->




                            <div class="form-group" id="birthday">
                                <label>身份证号</label>
                                <input name="inputText" type="text" id="person_id" class="form-control" ng-model="data.IDCode" ng-pattern="/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/">
                            </div>
                            <div class="form-group">
                                <label>信用代码</label>
                                <input type="text" class="form-control" ng-model="data.CreditID">
                            </div>
                            <div class="form-group">
                                <label>备注</label>
                                <input type="text" class="form-control" ng-model="data.Remarks">
                            </div>
                            <div class="form-group">
                                <label>评分</label>
                                <input type="text" class="form-control" ng-model="data.CreditScore">
                            </div>
                            <div class="form-group">
                                <label>客户专员</label>
                                <!--<select required class="form-control" ng-options="x.Name for x in userdata  track by x.Id" ng-model="data.ClientManager"></select>-->
                                <button class="form-control text-left" ng-model="data.ClientManager.Name" type="text" data-toggle="modal" data-target="#dlg">
                                    {{data.ClientManager.Name}}
                                </button>
                            </div>
                            <div class="form-group">
                                <label>状态：</label>
                                <label ng-if="data.ApprovalState == 0">等待审批</label>
                                <label ng-if="data.ApprovalState == 1">审批中</label>
                                <label ng-if="data.ApprovalState == 2">通过</label>
                                <label ng-if="data.ApprovalState == 3">已驳回</label>
                            </div>
                            <div class="form-group">
                                <label>户头</label>
                                <input type="text" disabled class="form-control" value="共{{data.Accounts.length}}个户头" />
                            </div>
                            <div class="form-group" ng-if="data.Accounts.length>0">
                                <label>户头列表</label>
                                <ul>
                                    <li ng-repeat="item in data.Accounts">
                                        {{item.Name}}--{{item.BankNumber}}
                                    </li>
                                </ul>
                            </div>
                            <div class="form-group" ng-if="Imgtitles.length >= 1">
                                <label>用户凭证：</label>
                                <img style="height: 50px; margin: 10px" ng-repeat="item in Imgtitles track by $index" ng-src="/file/customer/{{item.FileName}}" alt="用户凭证" ng-click="editReceipt(item)" data-toggle="modal" data-target="#tupian">
                            </div>
                        </div>
                        <footer class="panel-footer text-right bg-light lter">
                            <button type="button" class="btn btn-success" data-toggle="modal" data-target="#shangchuan">上传用户凭证</button>
                            <button type="button" class="btn btn-default" ng-click="$state.go('app.Customer_AccountList', { 'id': $stateParams.id })">户头管理</button>
                            <button type="button" class="btn btn-success" ng-click="edit()" ng-disabled="form.$invalid">修改</button>
                            <button type="button" class="btn btn-default" ng-click="abolish()">取消</button>
                        </footer>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="modal fade" id="dlg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">人员选择</h4>
                </div>
                <div class="modal-body">
                    <!--<input class="selectFind form-control" type="text" ng-model="searchTxt" placeholder="查找" />-->
                    <div class="input-group ">
                        <input class="selectFind form-control" type="search" ng-model="searchTxt" placeholder="查找">
                        <span class="glyphicon glyphicon-remove input-group-addon" ng-click="empty()"></span>
                    </div>
                    <div class="selectShow" style="width: 100%; height: 200px; border: 1px solid #CCCCCC; overflow: auto;">
                        <div style="cursor: pointer;" class="form-control" ng-click="selectEmp(x)" ng-repeat=" x in userdata|filter:{Name : searchTxt}">{{x.Name}}</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="tupian" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <!--<h4 class="modal-title" id="myModalLabe3">收货凭证 </h4>-->
                <!--<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>-->
                <img style="width: 100%;" ng-src="/file/customer/{{editReceiptdata}}" alt="用户凭证">
            </div>
        </div>
    </div>
</div>

<script>
    angular.module('app')//非标优惠
    .controller("Customer_EditC", function ($scope, $state, $http, $stateParams) {
        $scope.data = [
            District = ""
        ];
        console.log($stateParams.id);
        $scope.myurl = "../api/Upload/CustomerInfo?customerId=" + $stateParams.id;
        $http.get("/api/Customer/Detail", { params: { "id": $stateParams.id } })
           .then(function (res) {
               $scope.data = res.data;
               console.log($scope.data)
               //获取地市
               $scope.districts = JSON.parse(window.localStorage.getItem('districts'));
               if ($scope.data.District) {
                   for (var i = 0; i < $scope.districts.length; i++) {
                       if ($scope.districts[i].Id == $scope.data.District.Parent.Id) {
                           $scope.selectedDistrict1 = $scope.districts[i];
                           break;
                       }
                   }
               }
               $http.get("/api/Upload/GetCustomerInfo", { params: { "customerId": $stateParams.id } })
                       .then(function (res) {
                           console.log(res.data.Data);
                           $scope.Imgtitles = res.data.Data;
                       })
                       .then(function (error) {
                           if (error) {
                               alert("凭证获取失败");
                           }
                       });
           })
           .then(function (error) {
               if (error) {
                   alert("数据获取失败");
               }
           });
        //----------------------------动态加载联系人行  Start---------------------------------------------------------------------------------------------------------------------------------

        $scope.data.CustomerContacts = [{ ContactType: 0, ContactUser: "", ContactPhone: "" }];
        $scope.data.AccountantContacts = [{ ContactType: 1, ContactUser: "", ContactPhone: "" }];

        //新增联系人行
        $scope.AddContact = function () {

            $scope.obj = { ContactType: 0, ContactUser: "", ContactPhone: "" };
            $scope.data.CustomerContacts.push($scope.obj);
        };
        //新增财务联系人行
        $scope.AddAccountantContact = function () {

            $scope.obj = { ContactType: 1, ContactUser: "", ContactPhone: "" };
            $scope.data.AccountantContacts.push($scope.obj);
        };
        //删除联系人行
        $scope.remove = function (index) {

            if ($scope.data.CustomerContacts.length > 1) {
                $scope.data.CustomerContacts.splice(index, 1);
            } else {
                alert("最少要留有一个联系方式");
            }
        }

        $scope.removeAccountant = function (index) {

            if ($scope.data.AccountantContacts.length > 0) {
                $scope.data.AccountantContacts.splice(index, 1);
            } else {
                alert("已经没有可删除的行");
            }
        }
        //---------------------------End-----------------------------------------------------------------------------------------------------------------------------

        //身份证号码验证
        $("#person_id").change(function () {
            if ($("#person_id").val() != "") {
                //身份证的地区代码对照
                var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" };
                //获取证件号码
                var person_id = $("#person_id").val();
                //合法性验证
                var sum = 0;
                //出生日期
                var birthday;
                //验证长度与格式规范性的正则
                var pattern = new RegExp(/(^\d{15}$)|(^\d{17}(\d|x|X)$)/i);
                if (pattern.exec(person_id)) {
                    //验证身份证的合法性的正则
                    pattern = new RegExp(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/);
                    if (pattern.exec(person_id)) {
                        //获取15位证件号中的出生日期并转位正常日期
                        birthday = "19" + person_id.substring(6, 8) + "-" + person_id.substring(8, 10) + "-" + person_id.substring(10, 12);
                    }
                    else {
                        person_id = person_id.replace(/x|X$/i, "a");
                        //获取18位证件号中的出生日期
                        birthday = person_id.substring(6, 10) + "-" + person_id.substring(10, 12) + "-" + person_id.substring(12, 14);

                        //校验18位身份证号码的合法性
                        for (var i = 17; i >= 0; i--) {
                            sum += (Math.pow(2, i) % 11) * parseInt(person_id.charAt(17 - i), 11);
                        }
                        if (sum % 11 != 1) {
                            $(this).addClass("txtRequired");
                            alert("身份证号码不符合国定标准，请核对！");
                            //                                $("#person_id").val("");
                            //                                $("#person_id").css("border-color","red")
                            return form.$invalid = true;
                        }
                    }
                    //检测证件地区的合法性
                    if (aCity[parseInt(person_id.substring(0, 2))] == null) {
                        $(this).addClass("txtRequired");
                        alert("证件地区未知，请核对！");
                        return form.$invalid = true;
                    }
                    var dateStr = new Date(birthday.replace(/-/g, "/"));

                    //alert(birthday +":"+(dateStr.getFullYear()+"-"+ Append_zore(dateStr.getMonth()+1)+"-"+ Append_zore(dateStr.getDate())))
                    if (birthday != (dateStr.getFullYear() + "-" + Append_zore(dateStr.getMonth() + 1) + "-" + Append_zore(dateStr.getDate()))) {
                        $(this).addClass("txtRequired");
                        alert("证件出生日期非法！");
                        //                            $("#birthday").val("");
                        return form.$invalid = true;
                    }
                    $(this).removeClass("txtRequired");
                    //                        $("#birthday").val(birthday);
                }
                else {
                    $(this).addClass("txtRequired");
                    alert("证件号码格式非法！");
                    //$("#person_id").val("");
                    return form.$invalid = true;
                }

            } else {
                alert("请输入证件号！");
                $("#birthday").val("");
                //                    $("#person_id").css("border-color","red")
                return form.$invalid = true;
            }
            return form.$invalid = false;
        });
        $scope.edit = function () {
            console.log($scope.data);
            $http.put('../api/Customer', $scope.data)
                    .success(function () {
                        if (form.$invalid) {
                            alert("提交失败");
                        } else {
                            alert("提交成功");
                            if ($stateParams.action != '')
                                $state.go("app." + $stateParams.action);
                            else
                                $state.go("app.Customer_List");
                        }
                    });
        };
        //获取人员
        $http({
            url: "../api/Employee/List?name=&page=1&rows=9999",
            method: "GET"
        })
                .then(function (res) {
                    $scope.userdata = res.data.Data;
                })
                .then(function (error) {
                    if (error) {
                        console.log("error");
                    }
                });
        //返回上一页
        $scope.abolish = function () {
            console.log($stateParams.action);
            if ($stateParams.action = '')
                $state.go("app." + $stateParams.action);
            else
                $state.go("app.Customer_List");
        };
        $scope.selectEmp = function (emp) {
            $scope.data.ClientManager = emp;
            console.log($scope.data.ClientManager);
            $('#dlg').modal("hide");
        };
        $scope.empty = function () {
            $scope.searchTxt = "";
        };

        //查看大图
        $scope.editReceipt = function (item) {
            $scope.editReceiptdata = item.FileName;
        };
        $scope.bar = $(".bar");//进度条
        $scope.percent = $('.percent');//获取上传百分比
        $scope.showimg = $('.showimg');//显示图片的div
        $scope.progress = $('.progress');//显示进度的div
        $scope.files = $('.files');//文件上传控件的input元素
        $scope.btn = $('.btn span'); //按钮文本
        $scope.gosubmit2 = function () {
            $("#myupload").ajaxSubmit({
                dataType: 'json',//返回数据类型
                beforeSend: function () {
                    $scope.showimg.empty();
                    $scope.progress.show();
                    $scope.percentVal = '0%';
                    $scope.bar.width($scope.percentVal);
                    $scope.percent.html($scope.percentVal);
                    $scope.btn.html('上传中..');
                },
                //更新进度条事件处理代码
                uploadProgress: function (event, position, total, percentComplete) {
                    $scope.percentVal = percentComplete + '%';
                    $scope.bar.width($scope.percentVal);
                    $scope.percent.html($scope.percentVal);
                },
                success: function (data) {//图片上传成功时
                    if (data) {
                        alert("上传成功");
                        $('#shangchuan').modal("hide");
                        $state.go('app.Customer_Edit', {
                            id: $stateParams.id
                        });
                    }
                },
                error: function (xhr) {
                    $scope.btn.html('上传失败');
                    $scope.bar.width('0');
                    $scope.files.html(xhr.responseText);
                }
            });
        };
        function Append_zore(temp) {
            if (temp < 10) {
                return "0" + temp;
            }
            else {
                return temp;
            }
        }
    })
</script>